import 'package:flutter/material.dart';
import 'package:flutter_deer/account/account_router.dart';
import 'package:flutter_deer/account/widgets/withdrawal_account_item.dart';
import 'package:flutter_deer/mvp/base_page.dart';
import 'package:flutter_deer/mvp/power_presenter.dart';
import 'package:flutter_deer/res/gaps.dart';
import 'package:flutter_deer/routers/fluro_navigator.dart';
import 'package:flutter_deer/util/other_utils.dart';
import 'package:flutter_deer/widgets/fadein_text.dart';
import 'package:flutter_deer/widgets/my_app_bar.dart';
import 'package:flutter_deer/widgets/state_layout.dart';
import 'package:flutter_deer/account/models/user_bank_entity.dart';
import 'package:flutter_deer/account/iview/user_bank_iview.dart';
import 'package:flutter_deer/account/provider/user_bank_provider.dart';
import 'package:flutter_deer/account/presenter/user_bank_presenter.dart';
import 'package:flutter_gen/gen_l10n/deer_localizations.dart';
import 'package:lottie/lottie.dart';
import 'package:provider/provider.dart';
class UserBankListPage extends StatefulWidget {

  const UserBankListPage({super.key});

  @override
  _UserBankListPageState createState() => _UserBankListPageState();
}

class _UserBankListPageState extends State<UserBankListPage>  with BasePageMixin<UserBankListPage, PowerPresenter>, AutomaticKeepAliveClientMixin<UserBankListPage> implements UserBankIMvpView {

  final GlobalKey<AnimatedListState> _listKey = GlobalKey<AnimatedListState>();
  UserBankProvider provider = UserBankProvider();
  @override
  bool get wantKeepAlive => true;
  late UserBankPagePresenter userBankPagePresenter;

  @override
  PowerPresenter<dynamic> createPresenter() {
    final PowerPresenter<dynamic> powerPresenter = PowerPresenter<dynamic>(this);
    userBankPagePresenter = new UserBankPagePresenter();
    powerPresenter.requestPresenter([userBankPagePresenter]);
    return powerPresenter;
  }
  @override
  void setUserBankList(List<UserBankEntity>? userBankList){
    provider.setUserBankList(userBankList);
  }
  @override
  void saveUserBankSuccess(){

  }
  @override
  void saveUserBankFail(){

  }

  @override
  void removeUserBankSuccess(){
    Future.delayed(Duration(seconds: 1),()=>{userBankPagePresenter.fetchUserBankList()});
  }

  @override
  void initState() {
    super.initState();
    if(mounted) {
      Future.delayed(Duration(seconds: 1),()=>{userBankPagePresenter.fetchUserBankList()});
    }
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<UserBankProvider>(
        create: (_) => provider,
        child: Scaffold(
            appBar: MyAppBar(
                centerTitle: '${DeerLocalizations.of(context)!.bankCard.nullSafe}'
            ),
            body: Consumer<UserBankProvider>(
                builder: (_,provider,child){
                  if(provider.stateType==StateType.loading){
                    return StateLayout(type: StateType.loading);
                  }
                  return provider.userBankList!.isEmpty ? Center(
                    child: GestureDetector(
                      onTap: (){
                        NavigatorUtils.pushResult(context, AccountRouter.addWithdrawalAccountPage, (result) {
                          userBankPagePresenter.fetchUserBankList();
                        });
                      },
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Lottie.asset('assets/lottie/bank_card.json',width: MediaQuery.of(context).size.width/3,fit: BoxFit.cover),
                          Gaps.vGap8,
                          FadeInText(text:'${DeerLocalizations.of(context)!.addWithdrawalAccountNow.nullSafe}')
                        ],
                      ),
                    ),
                  ) :
                  AnimatedList(
                      key: _listKey,
                      padding: const EdgeInsets.only(top: 8.0),
                      initialItemCount: provider.userBankList!.length,
                      itemBuilder: (_, index, animation) =>
                          sizeItem(provider?.userBankList?.elementAt(index)??UserBankEntity(), index, animation,context)
                  );
                }
            )
        )
    );
  }

  Widget sizeItem(UserBankEntity data, int index, Animation<double> animation,BuildContext context) {
    /// item插入、移除动画
    return SizeTransition(
      axisAlignment: 1.0,
      sizeFactor: animation,
      child: WithdrawalAccountItem(
        key: ObjectKey(data), /// 这里注意必须添加key，原因见： https://weilu.blog.csdn.net/article/details/104745624
        data: data,
        onLongPress: () => (),
        useTap: true,
        onTap: () => NavigatorUtils.goBackWithParams(context, data)
      ),
    );
  }

  void _removeItem(String userBankId) {
    userBankPagePresenter.removeUserBank(userBankId);
  }

}
